/**
 * @author Tobias Boehm
 *
 * 24.03.2011
 */
package org.anddev.andengine.extension.tbsengine.scene;

import org.anddev.andengine.entity.layer.tiled.tmx.TMXLayer;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.extension.tbsengine.client.AbsInGameActivity;
import org.anddev.andengine.extension.tbsengine.map.AbsMap;
import org.anddev.andengine.extension.tbsengine.map.TBSmapConstants;
import org.anddev.andengine.extension.tbsengine.match.AbsClientMatch;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.input.touch.detector.ScrollDetector;
import org.anddev.andengine.input.touch.detector.ScrollDetector.IScrollDetectorListener;
import org.anddev.andengine.input.touch.detector.SurfaceScrollDetector;


/**
 * This is an abstract in-game scene for your implementation of {@link AbsInGameActivity}.
 * 
 * 
 * @author Tobias Boehm
 *
 * @param <MapClass>
 * @param <ClientMatchClass>
 */
public class AbsIngameScene<MapClass extends AbsMap,
							ClientMatchClass extends AbsClientMatch<?,?,MapClass,?>>

							extends Scene implements TBSmapConstants {

	/*private TMXTiledMap mTMXTiledMap;
	private final AbsInGameActivity mInGameActivity;*/
	
	protected final ClientMatchClass mClientMatch;
	protected final AbsInGameActivity mInGameActivity;
	
	private SurfaceScrollDetector mScrollDetector;

	
	public AbsIngameScene(final ClientMatchClass pClientMatch, final AbsInGameActivity pInGameActivity) {
		super(TBSE_SCENE_INGAME_LAYER_COUNT);
		
		this.mInGameActivity = pInGameActivity;
		
		this.mClientMatch = pClientMatch;
		
		// Load Tiled Map Layers
		this.loadTiledMapLayers(this.mClientMatch.getMap());
		
		// Load Gameobjects and higher level Components
		this.initHighLevelComponents(this.mClientMatch.getMap());
		
		// Init Camera Bounds
		this.initCameraBounds(this.mClientMatch.getMap());
		
		//this.initUnits();
		this.initScrollDetector();
	}

	private void initHighLevelComponents(final MapClass pMap) {
		// TODO Auto-generated method stub
		
	}

	private void loadTiledMapLayers(final MapClass pMap) {
				
		this.getFirstChild().attachChild(pMap.getGroundLayer());
		
		this.getFirstChild().attachChild(pMap.getDecorationLayer());
	}
	
	

	private void initScrollDetector() {
		
		this.mScrollDetector = new SurfaceScrollDetector(new IScrollDetectorListener() {
			
			@Override
			public void onScroll(ScrollDetector pScollDetector, TouchEvent pTouchEvent,
					float pDistanceX, float pDistanceY) {
				
				AbsIngameScene.this.mInGameActivity.getCamera().setCenter(
				AbsIngameScene.this.mInGameActivity.getCamera().getCenterX() + -pDistanceX, 
				AbsIngameScene.this.mInGameActivity.getCamera().getCenterY() + -pDistanceY);
			}
		});
		
		this.mScrollDetector.setEnabled(true);
		
		this.setOnSceneTouchListener(new IOnSceneTouchListener() {
			
			@Override
			public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
				
				return mScrollDetector.onSceneTouchEvent(pScene, pSceneTouchEvent);
			}
		});
	}
/*
	private void initUnits() {
		final int centerX = (AbsInGameActivity.CAMERA_WIDTH - this.mInGameActivity.getPlayerTextureRegion().getTileWidth()) / 2;
		final int centerY = (AbsInGameActivity.CAMERA_HEIGHT - this.mInGameActivity.getPlayerTextureRegion().getTileHeight()) / 2;
		
		final AnimatedSprite player = new AnimatedSprite(centerX, centerY, this.mInGameActivity.getPlayerTextureRegion());
		
		this.getLastChild().attachChild(player);
	}*/

	private void initCameraBounds(final MapClass pMap) {
		
		/* Make the camera not exceed the bounds of the TMXEntity. */
		final TMXLayer groundLayer = pMap.getGroundLayer();
		
		this.mInGameActivity.getCamera().setBounds(0, groundLayer.getWidth(), 0, groundLayer.getHeight());
		this.mInGameActivity.getCamera().setBoundsEnabled(true);
	}

	/*private void loadTMXTiledMap(final String pMapName) {
		
		try {
			final TMXLoader tmxLoader = new TMXLoader(this.mInGameActivity, this.mInGameActivity.getEngine().getTextureManager(), TextureOptions.BILINEAR_PREMULTIPLYALPHA, new ITMXTilePropertiesListener() {
				@Override
				public void onTMXTileWithPropertiesCreated(final TMXTiledMap pTMXTiledMap, final TMXLayer pTMXLayer, final TMXTile pTMXTile, final TMXProperties<TMXTileProperty> pTMXTileProperties) {
					 We are going to count the tiles that have the property "cactus=true" set. 
					if(pTMXTileProperties.containsTMXProperty("cactus", "true")) {
						
					}
				}
			});
			
			this.mTMXTiledMap = tmxLoader.loadFromAsset(this.mInGameActivity, "tmx/"+pMapName);

		//	Toast.makeText(this.mTBSExampleMain, "Map " + pMapName + "loaded...", Toast.LENGTH_LONG).show();
		} catch (final TMXLoadException tmxle) {
			Debug.e(tmxle);
		}
		
		final TMXLayer tmxLayer = this.mTMXTiledMap.getTMXLayers().get(0);
		this.getFirstChild().attachChild(tmxLayer);		
	}*/
	
	
}
